/*
 * @Description: 档案征集管理功能
 * @Author: Zzt
 * @Date: 2022-12-28 11:23:48
 * @LastEditTime: 2023-01-16 17:51:55
 */

import { useMemo } from 'react';
import { Button, Divider, Popover, Space } from 'antd';
import { DownOutlined } from '@ant-design/icons';
import { useArchiveCollectionContext } from '../../contexts/ArchiveCollectionContext';
import { useCollectionRouterTypeContext } from '../../contexts/CollectionRouterTypeContext';
import ImportBtn from '../optBtns/ImportBtn';
import AddArchiveItem from '../optBtns/AddArchiveItem';
import ExportOpt from '../optBtns/ExportOpt';
import PrintBtn from '../optBtns/PrintBtn';
import Circulation from '../optBtns/Circulation';
import Activate from '../optBtns/Activate';
import Destroy from '../optBtns/Destroy';
import Temporary from '../optBtns/Temporary';

interface IRecordOperatesProps {
  selectItems: number[];
}

const RecordOperates = (props: IRecordOperatesProps) => {
  const { selectItems } = props;
  const { routerType } = useCollectionRouterTypeContext();
  const { selectAT, deleteItem } = useArchiveCollectionContext();

  /* 转档案 */
  const circulation = useMemo(
    () =>
      selectAT && (
        <Circulation selectItems={selectItems} baseInfo={{ ...selectAT, dbName: 'collection' }} />
      ),
    [selectAT, selectItems]
  );

  /* 激活 */
  const activate = useMemo(
    () =>
      selectAT && (
        <Activate selectItems={selectItems} baseInfo={{ ...selectAT, dbName: 'collection' }} />
      ),
    [selectAT, selectItems]
  );

  /* 销毁 */
  const destroy = useMemo(
    () =>
      selectAT && (
        <Destroy selectItems={selectItems} baseInfo={{ ...selectAT, dbName: 'collection' }} />
      ),
    [selectAT, selectItems]
  );

  /* 暂存 */
  const temporary = useMemo(
    () =>
      selectAT && (
        <Temporary selectItems={selectItems} baseInfo={{ ...selectAT, dbName: 'collection' }} />
      ),
    [selectAT, selectItems]
  );

  const addArchiveItem = useMemo(
    () =>
      selectAT && (
        <AddArchiveItem
          coordinate={{
            atCode: selectAT?.atCode,
            atType: selectAT?.atType,
            level: selectAT?.atLevel
          }}
          onSave={undefined}
        />
      ),
    [selectAT]
  );

  const content = (
    <Space direction="vertical">
      {circulation}
      {activate}
      {destroy}
      {temporary}
    </Space>
  );
  return (
    <>
      <Space size="small">
        {addArchiveItem}
        <Button disabled={selectItems.length < 2}>批量修改</Button>
        <Button
          disabled={selectItems.length === 0}
          onClick={() =>
            deleteItem(1, {
              atCode: selectAT?.atCode,
              atType: selectAT?.atType,
              level: selectAT?.atLevel,
              entryIdList: selectItems,
              commonParams: {
                dbName: routerType
              }
            })
          }
        >
          删除
        </Button>
        <PrintBtn selectItems={selectItems} selectAt={selectAT} />
        <Button disabled>检测</Button>
        <Button disabled>查重</Button>
        <ImportBtn batchData={selectAT} />
        <ExportOpt
          info={{ ...selectAT, levelName: '', dbName: routerType, fondId: selectAT.fondsId }}
          selectItems={selectItems}
        />
        <Popover trigger="click" content={content}>
          <Button>
            流转
            <DownOutlined />
          </Button>
        </Popover>
        <Divider type="vertical" />
        <Button>筛选检索</Button>
      </Space>
    </>
  );
};
export default RecordOperates;
